/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * BaseSystemRelationController
 * 系统关联表
 * 修改纪录
 * 2021-01-27 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2021-01-27
 */
package com.topscomm.base.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.controller.auto.BaseSystemRelationControllerAuto;
import com.topscomm.base.pojo.BaseSystemRelationEntity;
import com.topscomm.dsp.api.DspServiceApi;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: zhanglongfei
 * @date: 2021-01-27
 * @description:系统关联表Controller
 * @modify:
 */
@RestController
@RequestMapping("base/systemRelation")
public class BaseSystemRelationController extends BaseSystemRelationControllerAuto implements IBaseSystemRelationController {
	@Autowired
	private DspServiceApi dspServiceApi;

	/**
	 * @author: zhanglongfei
	 * @date: 2021-01-27
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_sourcetype")) {
			String value = paraMap.get("qry_sourcetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourcetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_relationsourcetype")) {
			String value = paraMap.get("qry_relationsourcetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and relationsourcetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourceid")) {
			String value = paraMap.get("qry_sourceid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourceid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_relationsystem")) {
			String value = paraMap.get("qry_relationsystem").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and relationsystem = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @description:查询列表
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年1月27日下午3:05:07
	 * @modify:
	 */
	@GetMapping(value = "/queryList")
	public ResponseResult<Object> queryList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			List<Map<String, Object>> list = service.queryMapByWhere(sqlWhere);
			return ResponseResult.ok(list);
		} catch (Exception e) {
			return handleControllerException(e);
			// TODO: handle exception
		}
	}

	/**
	 * @description:保存数据
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年1月27日下午3:13:18
	 * @modify:
	 */
	@PostMapping(value = "/saveData")
	public ResponseResult<Object> saveData(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> dataMap = paraMap;
			boolean isAdd = false;
			BaseSystemRelationEntity entity = service.queryObjectByWhere("relationsystem='" + paraMap.get("relationsystem") + "' and relationsourcetype='" + paraMap.get("relationsourcetype")
					+ "' and relationsourceid='" + paraMap.get("relationsourceid") + "'");
			if (entity == null) {
				entity = new BaseSystemRelationEntity();
				isAdd = true;
			}
			entity.setEnabled(1);
			entity.convertFromMap(dataMap);
			Map<String, Object> map = new HashMap<>();
			map.put("sourcetype", paraMap.get("relationsourcetype"));
			map.put("id", paraMap.get("relationsourceid"));
			Page page = dspServiceApi.query("MCRQueryOABusiness", map);
			if (page.getTotalCount() == 0) {
				if (!isAdd) {
					entity.setEnabled(0);
					this.service.updateEntity(entity);
				}
				return ResponseResult.error("找不到相关单据");
			}
			Map<?, ?> relationMap = (Map<?, ?>) page.getDataList().get(0);
			entity.setLastupdatetime(new Date());
			entity.setLatestphase(ConvertUtil.convertToString(relationMap.get("LASTESTPHASE")));
			entity.setState(ConvertUtil.convertToString(relationMap.get("STATE")));
			entity.setUsercode(ConvertUtil.convertToString(relationMap.get("USERCODE")));
			entity.setUsername(ConvertUtil.convertToString(relationMap.get("USERNAME")));
			if (isAdd) {
				entity = this.service.insert(entity);
			} else {
				this.service.updateEntity(entity);
			}
			return ResponseResult.ok(entity);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @author: zhanglongfei
	 * @date: 2021-01-27
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}
}
